<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="../">
<head>
  <meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Protocols &mdash; The Logtalk Handbook v3.93.0-b01 documentation</title>
      <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=d75fae25" />
      <link rel="stylesheet" type="text/css" href="../_static/css/theme.css?v=19f00094" />
      <link rel="stylesheet" type="text/css" href="../_static/css/custom.css?v=396eccfe" />

  
  <!--[if lt IE 9]>
    <script src="../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
        <script src="../_static/jquery.js?v=5d32c60e"></script>
        <script src="../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
        <script src="../_static/documentation_options.js?v=c8100655"></script>
        <script src="../_static/doctools.js?v=9a2dae69"></script>
        <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
    <script src="../_static/js/theme.js"></script>
    <!-- begin favicon -->
    <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
    <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" />
    <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png" />
    <link rel="manifest" href="/site.webmanifest" />
    <link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5" />
    <meta name="msapplication-TileColor" content="#355b95" />
    <meta name="theme-color" content="#ffffff" />
    <!-- end favicon -->
    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Categories" href="categories.html" />
    <link rel="prev" title="Objects" href="objects.html" />
   
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >

          
          
          <a href="../index.html" class="icon icon-home">
            The Logtalk Handbook
              <img src="../_static/logtalk.gif" class="logo" alt="Logo"/>
          </a>
              <div class="version">
                3.93.0
              </div>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
    
              <p class="caption" role="heading"><span class="caption-text">Contents</span></p>
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal" href="index.html">User Manual</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="declarative.html">Declarative object-oriented programming</a></li>
<li class="toctree-l2"><a class="reference internal" href="features.html">Main features</a></li>
<li class="toctree-l2"><a class="reference internal" href="nomenclature.html">Nomenclature</a></li>
<li class="toctree-l2"><a class="reference internal" href="messages.html">Messages</a></li>
<li class="toctree-l2"><a class="reference internal" href="objects.html">Objects</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Protocols</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#defining-a-new-protocol">Defining a new protocol</a></li>
<li class="toctree-l3"><a class="reference internal" href="#finding-defined-protocols">Finding defined protocols</a></li>
<li class="toctree-l3"><a class="reference internal" href="#creating-a-new-protocol-at-runtime">Creating a new protocol at runtime</a></li>
<li class="toctree-l3"><a class="reference internal" href="#abolishing-an-existing-protocol">Abolishing an existing protocol</a></li>
<li class="toctree-l3"><a class="reference internal" href="#protocol-directives">Protocol directives</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#dynamic-protocols">Dynamic protocols</a></li>
<li class="toctree-l4"><a class="reference internal" href="#protocol-documentation">Protocol documentation</a></li>
<li class="toctree-l4"><a class="reference internal" href="#loading-files-into-a-protocol">Loading files into a protocol</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#protocol-relationships">Protocol relationships</a></li>
<li class="toctree-l3"><a class="reference internal" href="#protocol-properties">Protocol properties</a></li>
<li class="toctree-l3"><a class="reference internal" href="#implementing-protocols">Implementing protocols</a></li>
<li class="toctree-l3"><a class="reference internal" href="#built-in-protocols">Built-in protocols</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#the-built-in-protocol-expanding">The built-in protocol <code class="docutils literal notranslate"><span class="pre">expanding</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="#the-built-in-protocol-monitoring">The built-in protocol <code class="docutils literal notranslate"><span class="pre">monitoring</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="#the-built-in-protocol-forwarding">The built-in protocol <code class="docutils literal notranslate"><span class="pre">forwarding</span></code></a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#multi-threading-applications">Multi-threading applications</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="categories.html">Categories</a></li>
<li class="toctree-l2"><a class="reference internal" href="predicates.html">Predicates</a></li>
<li class="toctree-l2"><a class="reference internal" href="inheritance.html">Inheritance</a></li>
<li class="toctree-l2"><a class="reference internal" href="events.html">Event-driven programming</a></li>
<li class="toctree-l2"><a class="reference internal" href="threads.html">Multi-threading programming</a></li>
<li class="toctree-l2"><a class="reference internal" href="errors.html">Error handling</a></li>
<li class="toctree-l2"><a class="reference internal" href="reflection.html">Reflection</a></li>
<li class="toctree-l2"><a class="reference internal" href="programming.html">Writing and running applications</a></li>
<li class="toctree-l2"><a class="reference internal" href="printing.html">Printing messages and asking questions</a></li>
<li class="toctree-l2"><a class="reference internal" href="expansion.html">Term and goal expansion</a></li>
<li class="toctree-l2"><a class="reference internal" href="documenting.html">Documenting</a></li>
<li class="toctree-l2"><a class="reference internal" href="debugging.html">Debugging</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance.html">Performance</a></li>
<li class="toctree-l2"><a class="reference internal" href="installing.html">Installing Logtalk</a></li>
<li class="toctree-l2"><a class="reference internal" href="migration.html">Prolog integration and migration</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../refman/index.html">Reference Manual</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../faq/index.html">FAQ</a></li>
<li class="toctree-l1"><a class="reference internal" href="../devtools/index.html">Developer Tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">Libraries</a></li>
<li class="toctree-l1"><a class="reference internal" href="../ports/index.html">Ports</a></li>
<li class="toctree-l1"><a class="reference internal" href="../contributions/index.html">Contributions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../glossary.html">Glossary</a></li>
<li class="toctree-l1"><a class="reference internal" href="../bibliography.html">Bibliography</a></li>
<li class="toctree-l1"><a class="reference internal" href="../genindex.html">Index</a></li>
</ul>

    <p class="caption"><span class="caption-text">External Contents</span></p>
    <ul>
    <li class="toctree-l1"><a class="reference internal" href="../../apis/index.html">APIs</a></li>
    <li class="toctree-l1"><a class="reference internal" href="https://logtalk.org">Logtalk website</a></li>
    <li class="toctree-l1"><a class="reference internal" href="https://github.com/LogtalkDotOrg/logtalk3">GitHub repo</a></li>
    </ul>
  
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">The Logtalk Handbook</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../index.html" class="icon icon-home" aria-label="Home"></a></li>
          <li class="breadcrumb-item"><a href="index.html">User Manual</a></li>
      <li class="breadcrumb-item active">Protocols</li>
      <li class="wy-breadcrumbs-aside">
              <a href="https://github.com/LogtalkDotOrg/logtalk3/blob/master/docs/handbook/sources/userman/protocols.rst" class="fa fa-github"> Edit on GitHub</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <section id="protocols">
<span id="protocols-protocols"></span><h1>Protocols<a class="headerlink" href="#protocols" title="Link to this heading"></a></h1>
<p>Protocols enable the separation between interface and implementation:
several objects can implement the same protocol, and an object can
implement several protocols. Protocols may contain only predicate
declarations. In some languages the term <em>interface</em> is used with
a similar meaning. Logtalk allows predicate declarations of any scope
within protocols, contrary to some languages that only allow public
declarations.</p>
<p>Logtalk defines three built-in protocols,
<a class="reference external" href="../../apis/monitoring_0.html#monitoring-0" title="(in Logtalk APIs v3.93.0)"><span class="xref std std-ref">monitoring</span></a>,
<a class="reference external" href="../../apis/expanding_0.html#expanding-0" title="(in Logtalk APIs v3.93.0)"><span class="xref std std-ref">expanding</span></a>, and
<a class="reference external" href="../../apis/forwarding_0.html#forwarding-0" title="(in Logtalk APIs v3.93.0)"><span class="xref std std-ref">forwarding</span></a>, which are described at the
end of this section.</p>
<section id="defining-a-new-protocol">
<span id="protocols-defining"></span><h2>Defining a new protocol<a class="headerlink" href="#defining-a-new-protocol" title="Link to this heading"></a></h2>
<p>We can define a new protocol in the same way we write Prolog code: by
using a text editor. Logtalk source files may contain one or more
objects, categories, or protocols. If you prefer to define each entity
in its own source file, it is recommended that the file be named after
the protocol. By default, all Logtalk source files use the extension
<code class="docutils literal notranslate"><span class="pre">.lgt</span></code> but this is optional and can be set in the adapter files.
Intermediate Prolog source files (generated by the Logtalk compiler)
have, by default, a <code class="docutils literal notranslate"><span class="pre">_lgt</span></code> suffix and a <code class="docutils literal notranslate"><span class="pre">.pl</span></code> extension. Again, this
can be set to match the needs of a particular Prolog compiler in the
corresponding adapter file. For example, we may define a protocol named
<code class="docutils literal notranslate"><span class="pre">listp</span></code> and save it in a <code class="docutils literal notranslate"><span class="pre">listp.lgt</span></code> source file that will be
compiled to a <code class="docutils literal notranslate"><span class="pre">listp_lgt.pl</span></code> Prolog file (depending on the backend
compiler, the names of the intermediate Prolog files may include a
directory hash and a process identifier to prevent file name clashes
when embedding Logtalk applications or running parallel Logtalk processes).</p>
<p>Protocol names must be atoms. Objects, categories, and protocols share
the same namespace: we cannot have a protocol with the same name as an
object or a category.</p>
<p>Protocol directives are textually encapsulated by using two Logtalk
directives: <a class="reference internal" href="../refman/directives/protocol_1_2.html#directives-protocol-1-2"><span class="std std-ref">protocol/1-2</span></a> and
<a class="reference internal" href="../refman/directives/end_protocol_0.html#directives-end-protocol-0"><span class="std std-ref">end_protocol/0</span></a>. The
most simple protocol will be one that is self-contained, not depending
on any other Logtalk entity:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">protocol</span>(<span class="nv">Protocol</span>).
    ...
<span class="p">:- </span><span class="k">end_protocol</span>.
</pre></div>
</div>
<p>If a protocol extends one or more protocols, then the opening directive
will be:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">protocol</span>(<span class="nv">Protocol</span>,
    <span class="k">extends</span>([<span class="nv">Protocol1</span>, <span class="nv">Protocol2</span>, ...])).
    ...
<span class="p">:- </span><span class="k">end_protocol</span>.
</pre></div>
</div>
<p>In order to maximize protocol reuse, all predicates specified in a
protocol should relate to the same functionality. Therefore, the only
recommended use of protocol extension is when you need both a minimal
protocol and an extended version of the same protocol with additional,
convenient predicates.</p>
</section>
<section id="finding-defined-protocols">
<span id="protocols-finding"></span><h2>Finding defined protocols<a class="headerlink" href="#finding-defined-protocols" title="Link to this heading"></a></h2>
<p>We can find, by backtracking, all defined protocols by using the
<a class="reference internal" href="../refman/predicates/current_protocol_1.html#predicates-current-protocol-1"><span class="std std-ref">current_protocol/1</span></a> built-in predicate with a
unbound argument:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- current_protocol(Protocol).
</pre></div>
</div>
<p>This predicate can also be used to test if a protocol is defined by
calling it with a valid protocol identifier (an atom).</p>
</section>
<section id="creating-a-new-protocol-at-runtime">
<span id="protocols-creating"></span><h2>Creating a new protocol at runtime<a class="headerlink" href="#creating-a-new-protocol-at-runtime" title="Link to this heading"></a></h2>
<p>We can create a new (dynamic) protocol at runtime by calling the Logtalk
built-in predicate <a class="reference internal" href="../refman/predicates/create_protocol_3.html#predicates-create-protocol-3"><span class="std std-ref">create_protocol/3</span></a>:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- create_protocol(Protocol, Relations, Directives).
</pre></div>
</div>
<p>The first argument should be either a variable or the name of the new
protocol (a Prolog atom, which must not match an existing entity name).
The remaining two arguments correspond to the relations described in the
opening protocol directive and to the protocol directives.</p>
<p>For instance, the call:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- create_protocol(ppp, [extends(qqq)], [public([foo/1, bar/1])]).
</pre></div>
</div>
<p>is equivalent to compiling and loading the protocol:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">protocol</span>(ppp,
    <span class="k">extends</span>(qqq)).

<span class="p">    :- </span><span class="k">dynamic</span>.

<span class="p">    :- </span><span class="k">public</span>([foo<span class="o">/</span><span class="m">1</span>, bar<span class="o">/</span><span class="m">1</span>]).

<span class="p">:- </span><span class="k">end_protocol</span>.
</pre></div>
</div>
<p>If we need to create a lot of (dynamic) protocols at runtime, then it
is best to define a metaclass or a prototype with a predicate that will
call this built-in predicate in order to provide more sophisticated
behavior.</p>
</section>
<section id="abolishing-an-existing-protocol">
<span id="protocols-abolishing"></span><h2>Abolishing an existing protocol<a class="headerlink" href="#abolishing-an-existing-protocol" title="Link to this heading"></a></h2>
<p>Dynamic protocols can be abolished using the
<a class="reference internal" href="../refman/predicates/abolish_protocol_1.html#predicates-abolish-protocol-1"><span class="std std-ref">abolish_protocol/1</span></a> built-in predicate:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- abolish_protocol(Protocol).
</pre></div>
</div>
<p>The argument must be an identifier of a defined dynamic protocol;
otherwise an error will be thrown.</p>
</section>
<section id="protocol-directives">
<h2>Protocol directives<a class="headerlink" href="#protocol-directives" title="Link to this heading"></a></h2>
<p>Protocol directives are used to define protocol properties and
documentation.</p>
<section id="dynamic-protocols">
<span id="protocols-dynamic"></span><h3>Dynamic protocols<a class="headerlink" href="#dynamic-protocols" title="Link to this heading"></a></h3>
<p>As usually happens with Prolog code, a protocol can be either static or
dynamic. A protocol created during the execution of a program is always
dynamic. A protocol defined in a file can be either dynamic or static.
Dynamic protocols are declared by using the
<a class="reference internal" href="../refman/directives/dynamic_0.html#directives-dynamic-0"><span class="std std-ref">dynamic/0</span></a> directive in the protocol source code:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">dynamic</span>.
</pre></div>
</div>
<p>The directive must precede any predicate directives. Please be aware
that using dynamic code results in a performance hit when compared to
static code. We should only use dynamic protocols when these need to be
abolished during program execution.</p>
</section>
<section id="protocol-documentation">
<span id="protocols-documentation"></span><h3>Protocol documentation<a class="headerlink" href="#protocol-documentation" title="Link to this heading"></a></h3>
<p>A protocol can be documented with arbitrary user-defined information
by using the <a class="reference internal" href="../refman/directives/info_1.html#directives-info-1"><span class="std std-ref">info/1</span></a> entity directive. See the
<a class="reference internal" href="documenting.html#documenting-documenting"><span class="std std-ref">Documenting</span></a> section for details.</p>
</section>
<section id="loading-files-into-a-protocol">
<span id="protocols-include"></span><h3>Loading files into a protocol<a class="headerlink" href="#loading-files-into-a-protocol" title="Link to this heading"></a></h3>
<p>The <a class="reference internal" href="../refman/directives/include_1.html#directives-include-1"><span class="std std-ref">include/1</span></a> directive
can be used to load the contents of a file into a protocol. See the
<a class="reference internal" href="objects.html#objects-objects"><span class="std std-ref">Objects</span></a> section for an example of using this
directive.</p>
</section>
</section>
<section id="protocol-relationships">
<span id="protocols-relationships"></span><h2>Protocol relationships<a class="headerlink" href="#protocol-relationships" title="Link to this heading"></a></h2>
<p>Logtalk provides two sets of built-in predicates that enable us to query
the system about the relationships that a protocol has with other entities.</p>
<p>The <a class="reference internal" href="../refman/predicates/extends_protocol_2_3.html#predicates-extends-protocol-2-3"><span class="std std-ref">extends_protocol/2-3</span></a> built-in predicates return all
pairs of protocols so that the first one extends the second:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- extends_protocol(Protocol1, Protocol2).
</pre></div>
</div>
<p>or, if we also want to know the extension scope:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- extends_protocol(Protocol1, Protocol2, Scope).
</pre></div>
</div>
<p>To find which objects or categories implement which protocols, we can
call the <a class="reference internal" href="../refman/predicates/implements_protocol_2_3.html#predicates-implements-protocol-2-3"><span class="std std-ref">implements_protocol/2-3</span></a> built-in predicates:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- implements_protocol(ObjectOrCategory, Protocol).
</pre></div>
</div>
<p>or, if we also want to know the implementation scope:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- implements_protocol(ObjectOrCategory, Protocol, Scope).
</pre></div>
</div>
<p>Note that, if we use a non-instantiated variable for the first argument,
we will need to use the <a class="reference internal" href="../refman/predicates/current_object_1.html#predicates-current-object-1"><span class="std std-ref">current_object/1</span></a> or
<a class="reference internal" href="../refman/predicates/current_category_1.html#predicates-current-category-1"><span class="std std-ref">current_category/1</span></a>
built-in predicates to identify the kind of entity returned.</p>
</section>
<section id="protocol-properties">
<span id="protocols-properties"></span><h2>Protocol properties<a class="headerlink" href="#protocol-properties" title="Link to this heading"></a></h2>
<p>We can find the properties of defined protocols by calling the
<a class="reference internal" href="../refman/predicates/protocol_property_2.html#predicates-protocol-property-2"><span class="std std-ref">protocol_property/2</span></a> built-in predicate:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- protocol_property(Protocol, Property).
</pre></div>
</div>
<p>A protocol may have the property <code class="docutils literal notranslate"><span class="pre">static</span></code>, <code class="docutils literal notranslate"><span class="pre">dynamic</span></code>, or
<code class="docutils literal notranslate"><span class="pre">built_in</span></code>. Dynamic protocols can be abolished in runtime by calling
the <a class="reference internal" href="../refman/predicates/abolish_protocol_1.html#predicates-abolish-protocol-1"><span class="std std-ref">abolish_protocol/1</span></a>
built-in predicate. Depending on the <a class="reference internal" href="../glossary.html#term-backend-Prolog-compiler"><span class="xref std std-term">backend Prolog compiler</span></a>, a
protocol may have additional properties related to the source file where
it is defined.</p>
<p>The following protocol properties are supported:</p>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">static</span></code></dt><dd><p>The protocol is static</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">dynamic</span></code></dt><dd><p>The protocol is dynamic (and thus can be abolished in runtime by
calling the <a class="reference internal" href="../refman/predicates/abolish_category_1.html#predicates-abolish-category-1"><span class="std std-ref">abolish_category/1</span></a> built-in predicate)</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">built_in</span></code></dt><dd><p>The protocol is a built-in protocol (and thus always available)</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">source_data</span></code></dt><dd><p>Source data available for the protocol</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">file(Path)</span></code></dt><dd><p>Absolute path of the source file defining the protocol (if
applicable)</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">file(Basename,</span> <span class="pre">Directory)</span></code></dt><dd><p>Basename and directory of the source file defining the protocol (if
applicable); <code class="docutils literal notranslate"><span class="pre">Directory</span></code> always ends with a <code class="docutils literal notranslate"><span class="pre">/</span></code></p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">lines(BeginLine,</span> <span class="pre">EndLine)</span></code></dt><dd><p>Source file begin and end lines of the protocol definition (if
applicable)</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">directive(BeginLine,</span> <span class="pre">EndLine)</span></code></dt><dd><p>Source file begin and end lines of the protocol opening directive (if
applicable)</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">public(Resources)</span></code></dt><dd><p>List of public predicates and operators declared by the protocol</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">protected(Resources)</span></code></dt><dd><p>List of protected predicates and operators declared by the protocol</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">private(Resources)</span></code></dt><dd><p>List of private predicates and operators declared by the protocol</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">declares(Predicate,</span> <span class="pre">Properties)</span></code></dt><dd><p>List of <a class="reference internal" href="../refman/grammar.html#grammar-entity-properties"><span class="std std-ref">properties</span></a> for a predicate declared by the protocol</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">alias(Predicate,</span> <span class="pre">Properties)</span></code></dt><dd><p>List of <a class="reference internal" href="../refman/grammar.html#grammar-entity-properties"><span class="std std-ref">properties</span></a> for a <a class="reference internal" href="../glossary.html#term-predicate-alias"><span class="xref std std-term">predicate alias</span></a> declared by the protocol
(the properties include <code class="docutils literal notranslate"><span class="pre">for(Original)</span></code>, <code class="docutils literal notranslate"><span class="pre">from(Entity)</span></code>,
<code class="docutils literal notranslate"><span class="pre">non_terminal(NonTerminal)</span></code>, and <code class="docutils literal notranslate"><span class="pre">line_count(Line)</span></code> with <code class="docutils literal notranslate"><span class="pre">Line</span></code>
being the begin line of the alias directive)</p>
</dd>
</dl>
<p>Some of the properties, such as line numbers, are only available when the
protocol is defined in a source file compiled with the
<a class="reference internal" href="programming.html#flag-source-data"><span class="std std-ref">source_data</span></a> flag turned on.</p>
</section>
<section id="implementing-protocols">
<span id="protocols-implementing"></span><h2>Implementing protocols<a class="headerlink" href="#implementing-protocols" title="Link to this heading"></a></h2>
<p>Any number of objects or categories can implement a protocol. The syntax
is very simple:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">object</span>(<span class="nv">Object</span>,
    <span class="k">implements</span>(<span class="nv">Protocol</span>)).
    ...
<span class="p">:- </span><span class="k">end_object</span>.
</pre></div>
</div>
<p>or, in the case of a category:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">category</span>(<span class="nv">Object</span>,
    <span class="k">implements</span>(<span class="nv">Protocol</span>)).
    ...
<span class="p">:- </span><span class="k">end_category</span>.
</pre></div>
</div>
<p>To make all public predicates declared via an implemented protocol
protected or to make all public and protected predicates private we
prefix the protocol’s name with the corresponding keyword. For instance:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">object</span>(<span class="nv">Object</span>,
    <span class="k">implements</span>(private<span class="o">::</span><span class="nv">Protocol</span>)).
    ...
<span class="p">:- </span><span class="k">end_object</span>.
</pre></div>
</div>
<p>or:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">object</span>(<span class="nv">Object</span>,
    <span class="k">implements</span>(protected<span class="o">::</span><span class="nv">Protocol</span>)).
    ...
<span class="p">:- </span><span class="k">end_object</span>.
</pre></div>
</div>
<p>Omitting the scope keyword is equivalent to writing:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">object</span>(<span class="nv">Object</span>,
    <span class="k">implements</span>(public<span class="o">::</span><span class="nv">Protocol</span>)).
    ...
<span class="p">:- </span><span class="k">end_object</span>.
</pre></div>
</div>
<p>The same rules apply to protocols implemented by categories.</p>
</section>
<section id="built-in-protocols">
<span id="protocols-built-in"></span><h2>Built-in protocols<a class="headerlink" href="#built-in-protocols" title="Link to this heading"></a></h2>
<p>Logtalk defines a set of built-in protocols that are always available
for any application.</p>
<section id="the-built-in-protocol-expanding">
<span id="protocols-expanding"></span><h3>The built-in protocol <code class="docutils literal notranslate"><span class="pre">expanding</span></code><a class="headerlink" href="#the-built-in-protocol-expanding" title="Link to this heading"></a></h3>
<p>The built-in <a class="reference external" href="../../apis/expanding_0.html#expanding-0" title="(in Logtalk APIs v3.93.0)"><span class="xref std std-ref">expanding</span></a> protocol declares
the <a class="reference internal" href="../refman/methods/term_expansion_2.html#methods-term-expansion-2"><span class="std std-ref">term_expansion/2</span></a> and <a class="reference internal" href="../refman/methods/goal_expansion_2.html#methods-goal-expansion-2"><span class="std std-ref">goal_expansion/2</span></a>
predicates. See the description of the <a class="reference internal" href="programming.html#flag-hook"><span class="std std-ref">hook</span></a>
compiler flag for more details.</p>
</section>
<section id="the-built-in-protocol-monitoring">
<span id="protocols-monitoring"></span><h3>The built-in protocol <code class="docutils literal notranslate"><span class="pre">monitoring</span></code><a class="headerlink" href="#the-built-in-protocol-monitoring" title="Link to this heading"></a></h3>
<p>The built-in <a class="reference external" href="../../apis/monitoring_0.html#monitoring-0" title="(in Logtalk APIs v3.93.0)"><span class="xref std std-ref">monitoring</span></a> protocol declares the
<a class="reference internal" href="../refman/methods/before_3.html#methods-before-3"><span class="std std-ref">before/3</span></a> and <a class="reference internal" href="../refman/methods/after_3.html#methods-after-3"><span class="std std-ref">after/3</span></a> public event handler
predicates. See the <a class="reference internal" href="events.html#events-events"><span class="std std-ref">Event-driven programming</span></a> section for more details.</p>
</section>
<section id="the-built-in-protocol-forwarding">
<span id="protocols-forwarding"></span><h3>The built-in protocol <code class="docutils literal notranslate"><span class="pre">forwarding</span></code><a class="headerlink" href="#the-built-in-protocol-forwarding" title="Link to this heading"></a></h3>
<p>The built-in <a class="reference external" href="../../apis/forwarding_0.html#forwarding-0" title="(in Logtalk APIs v3.93.0)"><span class="xref std std-ref">forwarding</span></a> protocol declares the
<a class="reference internal" href="../refman/methods/forward_1.html#methods-forward-1"><span class="std std-ref">forward/1</span></a> user-defined message forwarding handler, which
is automatically called (if defined) by the runtime for any message that
the receiving object does not understand. See also the
<a class="reference internal" href="../refman/control/delegate_message_1.html#control-delegate-message-1"><span class="std std-ref">[]/1</span></a> control construct.</p>
</section>
</section>
<section id="multi-threading-applications">
<h2>Multi-threading applications<a class="headerlink" href="#multi-threading-applications" title="Link to this heading"></a></h2>
<p>When writing multi-threading applications, user-defined predicates calling
built-in predicates such as <code class="docutils literal notranslate"><span class="pre">create_protocol/3</span></code> and <code class="docutils literal notranslate"><span class="pre">abolish_protocol/1</span></code>
may need to be declared synchronized in order to avoid race conditions.</p>
</section>
</section>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="objects.html" class="btn btn-neutral float-left" title="Objects" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="categories.html" class="btn btn-neutral float-right" title="Categories" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 1998-2025, Paulo Moura.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>